/**
 * 
 */
package id.ac.gunadarma.paracorp.viewmodel;

import id.ac.gunadarma.paracorp.model.TranslatedSentence;
import id.ac.gunadarma.paracorp.services.UploadService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.util.media.Media;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.EventQueues;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.WireVariable;

/**
 * @author rezalesmana
 *
 */
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class UploadVM {
	

	@WireVariable
	private UploadService uploadService;
	
	private List<TranslatedSentence> listSentences;
	
	private List<TranslatedSentence> selectedListSentences;
	
	private String selectedLanguage = "ID";

	public List<TranslatedSentence> getListSentences() {
		return listSentences;
	}

	public void setListSentences(List<TranslatedSentence> listSentences) {
		this.listSentences = listSentences;
	}

	public List<TranslatedSentence> getSelectedListSentences() {
		return selectedListSentences;
	}

	public void setSelectedListSentences(List<TranslatedSentence> selectedListSentences) {
		this.selectedListSentences = selectedListSentences;
	}
	
	public String getSelectedLanguage() {
		return selectedLanguage;
	}

	public void setSelectedLanguage(String selectedLanguage) {
		this.selectedLanguage = selectedLanguage;
	}

	@NotifyChange({"listSentences", "selectedListSentences"})
	@Command
	public void processUpload(@BindingParam("files") Media[] files) {
		this.setListSentences(uploadService.processTranslateFiles(files, selectedLanguage));
		this.setSelectedListSentences(new ArrayList<TranslatedSentence>());
	}
	
	@NotifyChange({"listSentences", "selectedListSentences"})
	@Command
	public void remove() {
		this.getListSentences().removeAll(selectedListSentences);
		this.setSelectedListSentences(new ArrayList<TranslatedSentence>());
	}
	
	@Command
	public void save() {
		if(this.getListSentences() != null && this.getListSentences().size() > 0) {
			if(uploadService.upload(listSentences)) {
				Map<String, Object> args = new HashMap<String, Object>();
				args.put("language", this.getSelectedLanguage());
				BindUtils.postGlobalCommand(null, null, "uploadFinished", args);
			}
		}
	}
	
}
